//
//  HPConditionTextField.m
//  
//
//  Created by handpay on 13-1-22.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "HPConditionTextField.h"
#import "Utility.h"
#import "ZZTongDefine.h"

@interface HPConditionTextField ()

@property (nonatomic, retain) NSString *oldText;

@end

@implementation HPConditionTextField

- (void) dealloc
{
    [NOTIFICATION removeObserver:self];
    
    self.oldText = nil;
    
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void) awakeFromNib
{
    [self initialize];
}

- (void) initialize
{
    [NOTIFICATION addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:self];
    [NOTIFICATION addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:self];
    self.min = LONG_MIN;
    self.max = LONG_MAX;
    self.maxLength = INT32_MAX;
    self.conditionType = HPConditionType_ASCII;
    self.keyboardAppearance = UIKeyboardAppearanceAlert;
    self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.keyBoardExtraHeight = 0.0f;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (void) textChange:(NSNotification *)no
{
    if (no.object == self) {
//        [NOTIFICATION removeObserver:self];
        NSString *oText = [self.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *tText = @"";
        switch (self.conditionType) {
            case HPConditionType_Alphanumeric:{
//                if (![Utility checkChar:oText type:HPCharType_Alphanumeric]) {
//                    tText = [oText substringToIndex:oText.length];
//                }else{
//                    tText = oText;
//                }
                tText = [Utility convertString:oText toType:HPCharType_Alphanumeric];
                return;
                break;
            }
            case HPConditionType_CardNo: {
                if (oText.length > self.maxLength) {
                    oText = [oText substringToIndex:self.maxLength];
                }
                for (int i = 0; i < oText.length; i ++) {
                    if (i % 4 == 0 && i != 0) {
                        tText = [tText stringByAppendingFormat:@" "];
                    }
                    
                    tText = [tText stringByAppendingFormat:@"%@", [oText substringWithRange:NSMakeRange(i, 1)]];
                }
                break;
            }
            case HPConditionType_Money: {
                NSArray *array = [oText componentsSeparatedByString:@"."];
                NSString *beforPoint = [array objectAtIndex:0];
                
                if (beforPoint.length > self.maxLength - 3) {
                    beforPoint = [beforPoint substringToIndex:self.maxLength - 3];
                }
                if (array.count >= 2) {
                    NSString *decimal = [array objectAtIndex:1];
                    if (decimal.length >= 2) {
                        decimal = [decimal substringToIndex:2];
                    }
                    tText = [NSString stringWithFormat:@"%@.%@", beforPoint, decimal];
                } else {
                    tText = beforPoint;
                }
                
                break;
            }
            case HPConditionType_PositiveInteger: {
                NSArray *array = [oText componentsSeparatedByString:@"."];
                if (array.count > 1) {
                    tText = [NSString stringWithFormat:@"%@", [array objectAtIndex:0]];
                } else {
                    tText = oText;
                }
                if (self.maxLength != INT32_MAX && tText.length > self.maxLength) {
                    tText = self.oldText;
                }
//                long long number = [tText longLongValue];
//                if (number == 0) {
//                    tText = @"";
//                } else {
//                    tText = [NSString stringWithFormat:@"%lld", number];
//                }
                self.oldText = tText;
                break;
            }
            case HPConditionType_Numeric:
                if (self.maxLength != 0 && self.maxLength != INT32_MAX && oText.length > self.maxLength) {
                    tText = [oText substringToIndex:self.maxLength];
                } else {
                    tText = oText;
                }
                break;
            case HPConditionType_MaxLength: {
                if (self.maxLength != 0 && oText.length > self.maxLength) {
                    tText = [oText substringToIndex:self.maxLength];
                } else {
                    tText = oText;
                }
                break;
            }
            case HPConditionType_ASCII: {
                if (self.maxLength != 0 && oText.length > self.maxLength) {
                    tText = [oText substringToIndex:self.maxLength];
                } else {
                    tText = oText;
                }
                break;
            }
            case HPConditionType_Deafult:{
                tText = oText;
                break;
            }
        }
        if (![self.text isEqualToString:tText]){
            [super setText:tText];
        }
//        [NOTIFICATION addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:self];
//        [NOTIFICATION addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:self];
    }
}

- (void) textDidEndEditing:(NSNotification *)no
{
    if (no.object == self) {
//        [NOTIFICATION removeObserver:self];
        NSString *oText = [self.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        switch (self.conditionType) {
            case HPConditionType_Alphanumeric:{
//                BOOL isNumber = [self checkChar:oText type:HPCharType_Number];
//                BOOL isAlpha = [self checkChar:oText type:HPCharType_Alpha];
//                if (isAlpha && isNumber) {
//                }
                [super setText:oText];
                return;
                break;
            }
            case HPConditionType_CardNo: {
                break;
            }
            case HPConditionType_Money: {
                NSString* tempStr = [NSString stringWithFormat:@"%.2lf", [oText doubleValue]];
                if (![self.text isEqualToString:tempStr])  {
                    [super setText:tempStr];
                }
                
                break;
            }
            case HPConditionType_Numeric:
                break;
            case HPConditionType_PositiveInteger: {
//                if (oText.length > 0) {
//                    if (self.maxLength != INT32_MAX && oText.length > self.maxLength) {
//                        [super setText:[oText substringToIndex:self.maxLength]];
//                    } else {
//                        uint number = [oText intValue];
//                        if (number < self.min) {
//                            number = self.min;
//                        } else if (number > self.max) {
//                            number = self.max;
//                        }
//                        [super setText:[NSString stringWithFormat:@"%d", number]];
//                    }
//                } else {
//                    [super setText:@"1"];
//                }
                break;
            }
            case HPConditionType_MaxLength: {
                
                break;
            }
            case HPConditionType_ASCII: {
                
                break;
            }
            case HPConditionType_Deafult:{
                break;
            }
        }
//        [NOTIFICATION addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:self];
//        [NOTIFICATION addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:self];
    }
}

- (void) setText:(NSString *)text
{
    [super setText:text];
    [NOTIFICATION postNotificationName:UITextFieldTextDidChangeNotification object:self];
}

- (void) setConditionType:(HPConditionType)conditionType_
{
    _conditionType = conditionType_;
    switch (conditionType_) {
        case HPConditionType_Alphanumeric:{
             [self setKeyboardType:UIKeyboardTypeASCIICapable];
            break;
        }
        case HPConditionType_CardNo: {
            [self setKeyboardType:UIKeyboardTypeNumberPad];
            if (self.maxLength == INT32_MAX) {
                self.maxLength = 19;
            }
            break;
        }
        case HPConditionType_Numeric:
            [self setKeyboardType:UIKeyboardTypeNumberPad];
            break;
        case HPConditionType_Money: {
            [self setKeyboardType:UIKeyboardTypeDecimalPad];
            if (self.maxLength == INT32_MAX) {
                self.maxLength = 12;    // 带上小数点及后面两位小数不超过12位
            }
            break;
        }
        case HPConditionType_PositiveInteger: {
            [self setKeyboardType:UIKeyboardTypeNumberPad];
            break;
        }
        case HPConditionType_MaxLength: {
            
            break;
        }
        case HPConditionType_ASCII: {
            [self setKeyboardType:UIKeyboardTypeASCIICapable];
            break;
        }
        case HPConditionType_Deafult:{
            [self setKeyboardType:UIKeyboardTypeDefault];
//            [NOTIFICATION removeObserver:self];
            break;
        }
    }
}

- (CGFloat)getKeyBoardExtraHeight{
    return self.keyBoardExtraHeight;
}
@end
